<template>
  <div>
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :xs="7" :sm="6" :md="4" :lg="4" :xl="4">
        <el-tree
          :data="depts"
          :props="defaultProps"
          :expand-on-click-node="false"
          style="margin-top: 30px"
          default-expand-all
          @node-click="handleNodeClick"/>
      </el-col>
      <!--用户数据-->
      <el-col :xs="17" :sm="18" :md="20" :lg="20" :xl="20">
        <!--工具栏-->
        <div class="table_div">
          <!-- 搜索 -->
          <el-form ref="formSearch" :model="params" :inline="true" size="small">
            <el-form-item label="请输入用户名" prop="username">
              <el-input
                v-model="params.username"
                clearable
                class="filter-item"
                placeholder="支持模糊查询"
              />
            </el-form-item>
            <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toQuery">搜索</el-button>
            <el-button class="filter-item" size="mini" icon="el-icon-close" @click="resetSearchData">重 置</el-button>
            <el-button
              :loading="downloadLoading"
              size="mini"
              class="filter-item"
              type="warning"
              icon="el-icon-download"
              @click="download">导出</el-button>
          </el-form>
        </div>
        <div class="table_div">
          <el-button type="primary" size="mini" icon="el-icon-plus" @click="add">新 增</el-button>
        </div>
        <!--表格渲染-->
        <el-table
          :data="data"
          :height="tableHeight"
          border
          stripe
          highlight-current-row
          style="width: 100%"
          size="small"
        >
          <el-table-column prop="username" align="center" label="用户名"/>
          <el-table-column prop="mobile" align="center" label="电话"/>
          <el-table-column :show-overflow-tooltip="true" align="center" prop="email" label="邮箱"/>
          <el-table-column label="部门 / 岗位" align="center">
            <template slot-scope="scope">
              <!--<div>{{ scope.row.dept.name }} / {{ scope.row.job.name }}</div>-->
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center">
            <template slot-scope="scope">
              <div v-for="item in userStatusDicts" :key="item.value">
                <el-tag v-if="scope.row.status == item.value" :type="scope.row.value ? 'info' : ''">
                  {{ item.name }}
                </el-tag>
              </div>
            </template>
          </el-table-column>
          <el-table-column :show-overflow-tooltip="true" prop="createTime" align="center" label="创建日期"/>
          <el-table-column label="操作" width="125" align="center">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" icon="el-icon-edit" @click="edit(scope.row)"/>
              <el-popover
                :ref="scope.row.userId"
                placement="top"
                width="180">
                <p>确定删除本条数据吗？</p>
                <div style="text-align: right; margin: 0">
                  <el-button size="mini" type="text" @click="$refs[scope.row.userId].doClose()">取消</el-button>
                  <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.userId)">确定</el-button>
                </div>
                <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini"/>
              </el-popover>
            </template>
          </el-table-column>
        </el-table>
        <!--分页组件-->
        <el-pagination
          :total="totalCount"
          :current-page="currentPage + 1"
          style="margin-top: 8px;"
          layout="total, prev, pager, next, sizes"
          @size-change="sizeChange"
          @current-change="pageChange"/>
      </el-col>
    </el-row>
    <!--form 组件-->
    <eForm ref="form" :is-add="isAdd" :status-dicts="userStatusDicts"/>
  </div>
</template>

<script>
/* eslint-disable */
import initData from '@/mixins/initData'
import initDict from '@/mixins/initDict'
import {getDepts } from '@/api/dept'
import {del } from '@/api/user'
import eForm from './form'
import Config from '@/config'
export default {
  components: { eForm },
  mixins: [initData, initDict],
  data() {
    return {
      isAdd: false,
      delLoading: false,
      deptName: '',
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      downloadLoading: false
    }
  },
  created() {
    this.setHeight(250); //高度需要调整
    this.getDeptDatas();
    this.$nextTick(() => {
      this.getEnumList()
      this.initPageData()
    })
  },
  methods: {
    beforeInit() {
      this.url = 'user/listPage'
      return true
    },
    add() {
      this.isAdd = true
      const _this = this.$refs.form
      _this.getDepts()
      _this.dialog = true
    },
    edit(data) {
      this.isAdd = false
      const _this = this.$refs.form
      _this.getDepts()
      _this.form = data
      _this.form.status=data.status.toString();
      _this.dialog = true
    },
    subDelete(id) {
      this.delLoading = true
      del(id).then(res => {
        if (res.code == Config.code_200) {
          this.initPageData()
          this.$notify({title: '删除成功', type: 'success',})
        } else {
          this.$notify.error({title: '失败', message: res.msg})
        }
        this.delLoading = false
        this.$refs[id].doClose()
        this.dleChangePage()
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err.response.data.message)
      })
    },
    getDeptDatas() {
      getDepts().then(res => {
        if (res.code == Config.code_200) {
          this.depts = res.data
        } else {
          this.$notify.error({title: '获取部门失败', message: res.msg})
        }

      })
    },
    handleNodeClick(data) {
      if (data.id === 0) {
        this.params.deptId = null
      } else {
        this.params.deptId = data.id
      }
      this.initPageData()
    },
    // 导出
    download() {
      this.downloadLoading = true
      import('@/utils/export2Excel').then(excel => {
        const tHeader = ['ID', '用户名', '邮箱', '头像地址', '状态', '注册日期', '最后修改密码日期']
        const filterVal = ['id', 'username', 'email', 'avatar', 'enabled', 'createTime', 'lastPasswordResetTime']
        const data = this.formatJson(filterVal, this.data)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: 'table-list'
        })
        this.downloadLoading = false
      })
    },
  }
}
</script>

<style scoped>
  .table_div {
    padding: 8px;
    border: 1px solid #ebeef5;
    background-color: #fff;
    color: #303133;
    -webkit-transition: 0.3s;
    transition: 0.3s;
  }

</style >
